package com.sgcc.ln.pmos.e_ln_home.activity;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.ln.pmos.e_ln_home.adapter.SBDSBJXJHAdapter;

import com.sgcc.ln.pmos.e_ln_home.bean.SBDSBJXJHBean;
import com.sgcc.ln.pmos.e_ln_home.bean.SBDSBJXJHZHBean;
import com.sgcc.ln.pmos.e_ln_home.contract.SBDSBJXJHContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.SBDSBJXJHPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 输变电设备检修计划 sn_xhpl_sbjxjh_7_1
 */
@Route(path = HomeArouterPath.HOME_SBDSBJXJH)
public class SBDSBJXJHActivity extends MvpBaseRecycleActivity<SBDSBJXJHZHBean, SBDSBJXJHPresenter> implements SBDSBJXJHContract.View {

    private String mLabel, mInfoCode, dateTime;

    @Override
    protected SBDSBJXJHPresenter createPresenter() {
        return new SBDSBJXJHPresenter();
    }

    @Override
    public void getDataSuccess(SBDSBJXJHBean response) {
        if (response.getData().getIpConfigDataColsList() != null && response.getData().getObjectList() != null) {
            if (response.getData().getObjectList().getList() != null) {
                int total = response.getData().getObjectList().getTotal();
                int pageNum = response.getData().getObjectList().getPageNum();
                int pageSize = response.getData().getObjectList().getPageSize();
                boolean nextPage = false;
                if (total != 0) {
                    // 优先计算总页数
                    int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
                    //下一页等于当前页+1,但不能超过总页数界限
                    nextPage = pageNum + 1 <= totalPage ? true : false;
                }

                List<SBDSBJXJHBean.DataBean.ObjectListBean.ListBean> list = response.getData().getObjectList().getList();
                List<SBDSBJXJHZHBean> dataList = new ArrayList<>();
                for (SBDSBJXJHBean.DataBean.ObjectListBean.ListBean bean : list) {
                    SBDSBJXJHZHBean sbdsbjxjhzhBean = new SBDSBJXJHZHBean();
                    sbdsbjxjhzhBean.setGuid(bean.getGuid());
                    sbdsbjxjhzhBean.setDeviceName(bean.getDeviceName());
                    sbdsbjxjhzhBean.setvLevel(bean.getVLevel());
                    sbdsbjxjhzhBean.setBeginTime(bean.getBeginTime());
                    sbdsbjxjhzhBean.setEndTime(bean.getEndTime());
                    sbdsbjxjhzhBean.setPtype(bean.getPtype());
                    sbdsbjxjhzhBean.setUpdateTime(bean.getUpdateTime());
                    sbdsbjxjhzhBean.setApplyId(bean.getApplyId());
                    sbdsbjxjhzhBean.setBusinessTime(bean.getBusinessTime());
                    sbdsbjxjhzhBean.setMarketId(bean.getMarketId());


                    List<SBDSBJXJHZHBean.DataBean> dalist = new ArrayList();
                    List<SBDSBJXJHBean.DataBean.IpConfigDataColsListBean> list1 = response.getData().getIpConfigDataColsList();
                    for (int i = 0; i < list1.size(); i++) {
                        SBDSBJXJHZHBean.DataBean dataBean = new SBDSBJXJHZHBean.DataBean();
                        dataBean.setColName(list1.get(i).getColName());
                        dataBean.setColTitle(list1.get(i).getColTitle());
                        dalist.add(dataBean);
                        sbdsbjxjhzhBean.setData(dalist);
                    }
                    dataList.add(sbdsbjxjhzhBean);
                }
                loadSuccess(nextPage, dataList);
            } else {
                loadinglayout.showEmpty();
            }
        }else{
            loadinglayout.showError();
        }
    }

    @Override
    public void getDataFailed() {

    }

    @Override
    protected void setTitleBar(TitleBar title_bar) {
        title_bar.setTitle(mLabel);
        title_bar.tvRight.setTextSize(10);
        title_bar.setRightIconAndText(R.mipmap.down_jiantou, dateTime, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDateSelect();
            }
        });
    }

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        mLabel = getIntent().getStringExtra("label");
        mInfoCode = getIntent().getStringExtra("infoCode");
        dateTime = DateUtils.getCurrentTimes1();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new SBDSBJXJHAdapter(SBDSBJXJHActivity.this);
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getData(listSize, mInfoCode, dateTime, isloading);
    }

    private void showDateSelect() {
        Calendar startDate = Calendar.getInstance();
        startDate.set(2021, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2099, 11, 31);

        Calendar selectedDate = Calendar.getInstance();
        try {
            String dateStr = title_bar.tvRight.getText().toString().trim();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(dateStr);
            selectedDate.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DialogUtil.showDateSelectDialog(mContext,
                new boolean[]{true, true, true, false, false, false},
                startDate, endDate, selectedDate, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String string = TimeUtils.date2String(date, "yyyy-MM-dd");
                        title_bar.tvRight.setText(string);
                        dateTime = string;
                        refresh(true);
                    }
                }
        );
    }
}
